public with sharing class DbUtils {
	
	
	private static final String FROM_TABLE = ' FROM ' ;
	
	private static final String SELECT_QUERY_PREFIX = 'SELECT ' ;
		public static List<Sobject> fireQuery(String query){
		
		
		List<Sobject> result=null;
		if(null!= query){
		result=database.query(query);	
		}
		
		return result;
	}
	
	
	public static Savepoint beginTransaction(){
		Savepoint sp = Database.setSavepoint();
		return sp;
	}
	
	public static void commitTransaction(){
		//Database.
	}
	public static void rollabackTransaction(Savepoint sp){
		Database.rollback(sp);
	}
	
	public static Database.Deleteresult deleteByID(Id sObjectId){
		
		return deleteByID(sObjectId,false);
		 
	}
	
	public static Database.Deleteresult deleteByID(Id sObjectId, Boolean abortOnFailure){
		
		return Database.delete(sObjectId,abortOnFailure);
		 
	}
	
	public static Database.Deleteresult deleteRecord(Sobject record){
		
		return deleteRecord(record, false);
		 
	}
	
	public static Database.Deleteresult deleteRecord(Sobject record, Boolean abortOnFailure){
		
		return Database.delete(record,abortOnFailure);
		 
	}
	
	
	
	
	
	public static List<Database.Deleteresult> deleteIds(List<Id> sObjectIds){
		
		return deleteIds(sObjectIds,false);
		 
	}
	
	public static List<Database.Deleteresult> deleteIds(List<Id> sObjectIds , Boolean abortOnFailure){
		
		return Database.delete(sObjectIds,false);
		 
	}
	
	
	
	
	public static List<Database.Deleteresult> deleteRecords(List<Sobject> records){
		
		return deleteRecords(records, false);
		 
	}
	
	public static List<Database.Deleteresult> deleteRecords(List<Sobject> records, Boolean abortOnFailure){
		
		return Database.delete(records,abortOnFailure);
		 
	}
	
	public static List<Sobject> fetchRecords(DatabaseObject databaseObject){
		
		List<Sobject> result=null;
		if( null!=databaseObject){
			
			String queryString = prepareSelectQuery(databaseObject);
			
			if(null!=queryString ){
				
			result=	fireQuery(queryString);
			}
		}
		return result;
	}
	
		
	
	
	private static String prepareSelectQuery(DatabaseObject databaseObject){
		
		String query= null;
		
		//Preparing select clause
		if(null!=databaseObject.getFieldToSelect() && 
						databaseObject.getFieldToSelect().size()>0) {
			query=	SELECT_QUERY_PREFIX + prepareSelectString(databaseObject.getFieldToSelect());
		}
		
		//Preparing From table clause
		if(null!= databaseObject.getCriteriaObjectName()){
			query = query + FROM_TABLE + databaseObject.getCriteriaObjectName();
		}
		System.debug('prepareQuery : ' +query);
		return query;
	}
	
	private static String prepareWhereClause(List<Criteria> criteriaList){
		
		return null;
	}
	
	private static String prepareSelectString(Set<String> selectFields){
		
		String selectString='';
		if(null!=selectFields){
		selectString=	prepareCommaSeparatedString(selectFields, null, null);
		}
		System.debug('prepareSelectString : ' +selectString);
		return selectString;
	}
	
	
	public static String prepareCommaSeparatedString(Set<String> selectFields, 
							String startingBrackets,String closingBrackets){
	
	
	if(null == selectFields && selectFields.size() <= 0){
	return startingBrackets + closingBrackets;
	}
	
	String returnValue = (null!= startingBrackets? startingBrackets + ' ':'');
	Boolean isFirst = true;
	for(String anObject : selectFields){
	if(isFirst){
		returnValue+=anObject;
	isFirst = false;
	} else {
	returnValue += ',';
	returnValue += anObject;
	}
	}
	returnValue = (null!= closingBrackets? returnValue + ' ' + closingBrackets: returnValue );
	System.debug('Select string: ' + returnValue);
	
	return returnValue;
	
	}
	
		
}